package com.oyjd.business.movepage.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.oyjd.R;
import com.oyjd.framework.log.MyLog;
import com.oyjd.framework.util.ImageHelp;

public class ViewPageDemo extends Activity {
	private static final String TAG = "ViewPageDemo";

	private ViewPager viewPager;
	private TextView tv_pic_name;
	private TextView tv_pic_pos;
	private List<String> datas = new ArrayList<String>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_viewpage_demo);
		for (int i = 0; i < 10; i++) {
			datas.add("http://img.itc.cn/photo/jNwSG9BArwb");
		}

		tv_pic_name = (TextView) this.findViewById(R.id.tv_pic_name);
		tv_pic_pos = (TextView) this.findViewById(R.id.tv_pic_pos);
		viewPager = (ViewPager) this.findViewById(R.id.vp_imgs);
		viewPager.setAdapter(pa);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				initPicInfo(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		//
		initPicInfo(0);
		viewPager.setCurrentItem(0);
	}

	private void initPicInfo(int pos) {
		tv_pic_name.setText("索马里海盗" + (pos + 1));
		tv_pic_pos.setText((pos + 1) + "/" + datas.size());
	}

	private PagerAdapter pa = new PagerAdapter() {

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			MyLog.i(TAG, "destroyItem-----" + arg1);
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			MyLog.i(TAG, "instantiateItem-----" + arg1);
			String url = datas.get(arg1);
			View view = createView(url);
			((ViewPager) arg0).addView(view);
			return view;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}
	};

	/**
	 * 创建视图
	 * 
	 * @param url
	 * @return
	 */
	private View createView(String url) {
		LinearLayout ll = new LinearLayout(this);
		ll.setGravity(Gravity.CENTER);
		ll.setBackgroundColor(Color.BLACK);
		ImageView img = new ImageView(this);
		img.setPadding(2, 2, 2, 2);
		img.setBackgroundColor(Color.WHITE);
		ImageHelp.doImg(img, url);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(5, 0, 5, 0);
		ll.addView(img, lp);
		return ll;
	}
}
